package com.charm.information.adapter

import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import com.alibaba.android.arouter.launcher.ARouter
import com.charm.information.entity.ChannelEntity

/**
 * Created by tools on 2018/1/4.
 */
class ChannelAdapter(fm: FragmentManager, channelList: ArrayList<ChannelEntity>) : FragmentPagerAdapter(fm) {
    private var mChannelList: ArrayList<ChannelEntity> = channelList
    private var fragments: HashMap<Int, Fragment> = HashMap()

    override fun getItem(position: Int): Fragment {
        fragments[position]?:
            fragments.put(position, ARouter.getInstance().build("/information/eastHeadLine").withString("catid", mChannelList[position].catid).navigation() as Fragment)
        return fragments[position] as Fragment
    }

    override fun getCount(): Int = mChannelList?.let {
        mChannelList.size
    }

    override fun getPageTitle(position: Int): CharSequence {
        return if (mChannelList != null && position < mChannelList.size) {
            mChannelList[position].name
        } else {
            position.toString() + ""
        }
    }
}